
* EVENT STUDY STOCK OF RICH TAXPAYERS *

cd "$mypathRR/Results/"

* * *   * * *   * * *   * * *   * * *   * * *
* PREPARE DATA FOR EVENT STUDY 
* * *   * * *   * * *   * * *   * * *   * * *

local b " 55 60 60 60"
local t " 75 80 85 95"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"


* ALL RESIDENTS, INCOME CONTROL GROUP 
preserve
drop if year>2010



collapse (count) Residents=persid (mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(year treat_i)
label var Residents "Residents"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var year "Year"

sort treat_i year

gen Period=(year>=2006)
gen DiD=treat_i*Period
gen weight=Residents

drop if DiD==.


* Gen interaction terms for pre-treatment periods
forval n=1/5 {
local i= 2006 -`n'
gen pre`n'=0
replace pre`n'= 1 if treat_i == 1 & year == `i'
label var pre`n' "`i' (treatment lag `n')"
}
replace pre1 = 0 // make 2005 the reference year


* Gen interaction terms for post-treatment periods
forval n=1/5 {
local i = 2005 + `n'
gen post`n' = 0
replace post`n' = 1 if treat_i == 1 & year == `i'
label var post`n' "`i' (treatment lead `n')"
}

* Gen group-specific time trends
	gen group_0 = treat_i == 0
	gen group_1 = treat_i == 1
	foreach var of varlist group_*  {
	gen trend_`var' = year*`var'
	label var trend_`var' "Group-specific time trend"
	}
	gen trend = year
	
gen ln_Residents = ln(Residents)



* * *   * * *   * * *   * * *   * * *   * * *
* EVENT STUDY GRAPHS
* * *   * * *   * * *   * * *   * * *   * * *


* make lists of coefficients
global coeflist " pre5 pre4 pre3 pre2 pre1 post1 post2 post3 post4 post5 " //2001-2010
*global coeflist " pre5 pre4 pre3 pre2 pre1 post1 post2 " //2001-2007

// change outcomes as needed
global outcomes "Residents ln_Residents"


foreach outcome in $outcomes {
	sum `outcome' [aweight = weight] if treat_i == 1 & year == 2005
	global `outcome'2005 = `r(max)'
reghdfe `outcome' $coeflist [aweight = weight], vce(robust) noabsorb
		est sto model_`outcome'_`x'_`y'
}	

	restore
}


	coefplot (model_Residents_55_75, label(" ") keep($coeflist ) omitted color(navy*0.55) ciopts(recast(rarea) color(navy*0.05) color(navy%10)))  ///
			 (model_Residents_60_95, label(" ") keep($coeflist ) omitted color(maroon*0.65) ciopts(recast(rarea) color(maroon*0.01) color(maroon%10)))  ///				
			 (model_Residents_60_85, label(" ") keep($coeflist ) omitted color(midblue*0.6) ciopts(recast(rarea) color(midblue*0.1) color(midblue%10)))  ///
			 (model_Residents_60_80, label(" ") keep($coeflist ) omitted color(midblue*1.1) ciopts(recast(rarea) color(midblue*0.2) color(midblue%20)))  ///
			 , ///
					order($coeflist ) ///
					coeflabels(pre5 = "2001" pre4 = "2002" pre3 = "2003" pre2 = "2004" pre1 = "2005" ///
					post1 = "2006" post2 = "2007" post3 = "2008" post4 = "2009" post5 = "2010") ///
					vertical levels(95) ciopts(recast(rarea)) connect(direct) nooffsets ///
					yline(0, lcolor(black))  xline(5.5, lcolor(red)) xline(7.5, lcolor(green)) ///
					legend(title("Control group (% of threshold)", size(small)) ///
						order(2 "55-75%" 8 "60-80%" 6 "60-85%" 4 "60-95%") row(2) ring(0) position(11) bmargin(vsmall) size(small)) ///
					xlabel(, labsize(small) ang(45)) ylabel(-250(50)250, labsize(small) grid) ///
					ytitle("Residents (treated - control)") name(gr_Residents_all, replace) 
	graph export "Fig_6a)-ES_Residents-all_ctrl_groups.pdf", as(pdf)	replace

